/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: COFFEEWEN
 * Date: 2022-09-29
 * Time: 20:48
 */
public class TreeBinary {
    static class TreeNode{
        char val;
        TreeNode left;
        TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;

        return A;
    }
    public int maxDepth2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = maxDepth2(root.left);//2   -1
        int rightHeight = maxDepth2(root.right);//0  3
        if(leftHeight >= 0 && rightHeight >= 0 &&
                Math.abs(leftHeight-rightHeight) <= 1) {
            return Math.max(leftHeight,rightHeight) + 1;
        }else {
            return -1;
        }
    }
    public boolean isBalanced2(TreeNode root) {
        if(root == null) return true;
        return maxDepth2(root) >= 0;
    }
}
